using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows;
using SBPweb.Themes.Windows.Skins;

namespace SBPweb.Controls.Windows.Common
{
	/// <summary>
	/// Class of Label
	/// </summary>
	[ToolboxBitmap(typeof(System.Windows.Forms.Label))]
	public partial class Label : System.Windows.Forms.Label, ISkinnable
	{
		#region Private members

		private bool mUseDefaultSkin;
		private Skin mSkin;
		private Color mNoSkinBackColor;
		private Color mNoSkinForeColor;

		#endregion

		#region Constructor logic

		public Label()
		{
			mNoSkinBackColor = BackColor;
			mNoSkinForeColor = ForeColor;
			SetStyle();
			InitializeComponent();
			RegisterEventHandlers();
		}

		public Label(Skin skin)
		{
			mNoSkinBackColor = BackColor;
			mNoSkinForeColor = ForeColor;
			SetStyle();
			mSkin = skin;
			InitializeComponent();
			RegisterEventHandlers();
		}

		private void SetStyle()
		{
			base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
		}

		private void DoInvalidate()
		{
			if (Skin != null)
			{
				if (this.BackColor != Color.Transparent)
				{
					BackColor = Skin.CachedProperties.ControlColor;
				}
				ForeColor = Skin.CachedProperties.ControlTextColor;
			}
			else
			{
				BackColor = mNoSkinBackColor;
				ForeColor = mNoSkinForeColor;
			}
			Invalidate();
		}

		#endregion

		#region Custom draw

		protected override void OnPaint(PaintEventArgs e)
		{
			if (Skin != null)
			{
				e.Graphics.TextRenderingHint = Skin.TextRenderingHint;
			}
			base.OnPaint(e);
		}

		#endregion

		#region Events

		private void RegisterEventHandlers()
		{
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
		}

		void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			DoInvalidate();
		}

		#endregion

		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				Invalidate();
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				Invalidate();
			}
		}

		#endregion
	}
}
